<?php

namespace App\HttpController;


use App\Common\User\WxUser;
use App\Common\Web\CookieModel;
use App\X\EHelper;
use Carbon\Carbon;
use XWX\Common\H;

class ControllerWeb extends ControllerBase
{
    /**
     * @return CookieModel
     * @throws \Exception
     */
    public function funcAppCookie()
    {
        $app_cookie = $this->context()->get(__METHOD__);
        if ($app_cookie == null)
        {
            $cookies = $this->request()->getCookieParams();
            $app_cookie = new CookieModel($cookies);

            $this->context()->set(__METHOD__, $app_cookie);
        }

        return $app_cookie;
    }


    public function getUserID()
    {
        return $this->funcAppCookie()->getUserId();
    }

    public function getAppID()
    {
        return $this->funcAppCookie()->getAppId();
    }

    public function getWord()
    {
        return $this->funcAppCookie()->getWord();
    }


    /**
     * 通用页面跳转
     *
     * @return string
     * @throws \Exception
     */
    public function funcCommonPageName()
    {
        $app_cookie = $this->funcAppCookie();
        $wx_word = $app_cookie->funcGetWxWord();

        //默认
        $page_name = 'index.html';

        //自定义页面，例：page:页面名
        if (H::funcStrStartsWith($wx_word->suffix(), 'page'))
        {
            $suffix_data = explode(':', str_replace(',', ':', $wx_word->suffix()));
            $page_name = $suffix_data[1];

            $page_query = '';
            if (count($suffix_data) > 2)
            {
                $p_index = 0;
                for ($i = 2; $i < count($suffix_data); $i++)
                {
                    $page_query .= "&p{$p_index}={$suffix_data[$i]}";

                    $p_index++;
                }

                $page_query = '?__a=1' . $page_query;
            }

            $page_name = "{$page_name}.html{$page_query}";
        }


        return $page_name;
    }


    protected function onRequest(?string $action): ?bool
    {
        //检查访问环境
        if (!$this->funcEnvIsWeixin())
        {
            $this->funcReturnViewSysInfo('请在微信客户端打开链接', '');
            return false;
        }

        //cookie验证
        $app_cookie = $this->funcAppCookie();
        if (H::funcStrIsNullOrEmpty($app_cookie->x()))
        {
            $this->funcReturnViewSysInfo('访问超时，请重新授权', 'x null or empty');
            return false;
        }

        $this->funcSetX($app_cookie->x());

        if (!$app_cookie->funcCheckSign($this->funcGetX__config()->aconfig_app_secret))
        {
            //跳微信授权
            $this->funcReturnViewSysInfo('签名无效', 'getSign is error', 'weui-icon-warn');
            return false;
        }


        $expire_m = 60;
        if (EHelper::funcIsDev())
        {
            $expire_m = 5 * 60;
        }

        $ts = Carbon::createFromTimestamp($app_cookie->getTimestamp());
        if (H::now()->diffInMinutes($ts) > $expire_m)
        {
            $this->funcReturnViewSysInfo('访问超时，请重新授权', 'getTimestamp expire');
            return false;
        }

        //权限验证
//        $app_cookie->getAppId();


        return parent::onRequest($action);
    }
}